// pages/login/phone.js
const common = require('../../public.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: '',
        sms:"",
        show: true,
        flags:""
    },

    /**
     * 生命周期函数--监听页面加载
     */

    //验证码
    onChsms(event){
        this.setData({
           sms:event.detail
        })
    },
        // 切换页面
        click_yan(){
          wx.navigateTo({
            url: '/pages/login/password',
          })
          },
    // 登录
    click_login(){
        // 提交按钮
        var that = this
        if (that.data.value == '') {
          wx.showToast({
            title: '请填写手机号',
            icon: 'none'
          })
          return
        }
        //   if(this.data.flag){
            if (that.data.sms == '') {
              wx.showToast({
                title: '请填写验证码',
                icon: 'none'
              })
              return
            }
            if(this.data.flags==1){   //忘记密码
               wx.navigateTo({
                 url: '/pages/login/newword?telephone=' +this.data.value +'&sms=' + this.data.sms,
               }) 
               return
            }
                const tempJson ={
                  telephone:that.data.value,
                  msgCode:that.data.sms,
                }
                common.wxRequest("/user/phone/login", "post", tempJson, 'application/x-www-form-urlencoded', function (res) {
                   if(res.status==200){
                    wx.setStorage({
                        key: 'user',
                        data: res.data,
                        success: function () {
                          // resolve(true)
                        }
                      })
                 that.data.backUrl=='undefined' ?  wx.redirectTo({url: '/pages/index/index'}): wx.redirectTo({url:that.data.backUrl  })
                
                }else{
                       wx.showToast({
                         title: res.msg,
                         icon:"none"
                       })
                   }
                  })    
                  return
                },
            
    // },
    // 发验证码的接口
    sms_fn(){
        var that = this
        var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
        const numy= myreg.test(this.data.value)
        if(numy){
            const param = {
                telephone:this.data.value,
                timestamp: 1519 + Math.floor(Math.sqrt(this.data.value)) +'.0'
              }
              // 链接
              var url = this.data.flags==1 ? '/user/forget/msgcode' : '/user/login/msgcode'
              common.post(url,
                param,
                res => {
                  if (res.status === 200) {
                    that.getCodes()
                    wx.showToast({
                      title: res.msg,
                    })
                  }else{
                    wx.showToast({
                        title: res.msg,
                        icon: 'none'
                      })
                }
            })
            return
           }else{
            wx.showToast({
                title: "请输入正确得手机号",
                icon: 'none'
              }) 
            return
           }
     },
     getCodes(){
        const TIME_COUNT = 60;
        if (!this.data.timer) {
            this.setData({
                count :TIME_COUNT,
                show:false,
            })
          this.timer = setInterval(() => {
          if ( this.data.count > 0 &&  this.data.count <= TIME_COUNT) {
          this.data.count--
            this.setData({
                count:this.data.count
            })
            console.log(this.data.count)
           } else {
            this.setData({
                show:true
            })
            clearInterval(this.timer);
            this.setData({
                timer:null
            })
          
           }
          }, 1000)
         }
      } ,
      
    onChange(event) {
        this.setData({
            value:event.detail
        })
      },
    onLoad: function (options) {
      console.log(options.backUrl,"858888")
      if (options.backUrl) {
        wx.setStorage({
          key: 'backUrl',
          data: options.backUrl,
          success: function () {
          }
        })
        this.data.backUrl = decodeURIComponent(options.backUrl)
        return
      }else{
        this.data.backUrl = '/pages/index/index'
      
      }
      this.setData({
        flags:options.id ? options.id :''

      })
    },
})